package com.ja.openchat.viewmodels;

import android.app.Application;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.blankj.utilcode.util.ToastUtils;
import com.ja.openchat.R;
import com.liaobeicms.http.api.ApiServer;
import com.liaobeicms.http.bean.BaseResponse;
import com.liaobeicms.http.bean.UploadImageBean;
import com.liaobeicms.http.net.RetrofitManager;
import com.zy.openchat.core.model.user.UserInfo;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseViewModel;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;


public class NameAuthenNumberViewModel extends BaseViewModel {
    private final Map<String, String> imageAddress = new HashMap<>();

    private String fontPhoto;
    private String fontPhotoAddress;
    private String backPhoto;
    private String backPhotoAddress;
    private String username;
    private String idcardno;

    public NameAuthenNumberViewModel(@NonNull Application application) {
        super(application);
    }

    public void submitData(String fontPhoto, String backPhoto, String username, String idcardno) {
        if (TextUtils.isEmpty(username)) {
            ToastUtils.showShort("请输入真实姓名");
            return;
        }
        if (TextUtils.isEmpty(idcardno)) {
            ToastUtils.showShort("请输入身份证号码");
            return;
        }
        if (TextUtils.isEmpty(fontPhoto)) {
            ToastUtils.showShort("请添加身份证正面照片");
            return;
        }
        if (TextUtils.isEmpty(backPhoto)) {
            ToastUtils.showShort("请添加身份证反面照片");
            return;
        }
        this.fontPhoto = fontPhoto;
        this.backPhoto = backPhoto;
        this.username = username;
        this.idcardno = idcardno;
        uploadFontImage(fontPhoto);
    }

    public void uploadFontImage(String imagePath) {
        if (imagePath.contains("http")) {
            fontPhotoAddress = imagePath;
            uploadBackImage(backPhoto);
            return;
        }
        fontPhotoAddress = imageAddress.get(fontPhoto);
        if (!TextUtils.isEmpty(fontPhotoAddress)) {
            uploadBackImage(backPhoto);
            return;
        }
        showProgress();
        File file = new File(imagePath);
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        RequestBody logoRequestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        builder.addFormDataPart("file", file.getName(), logoRequestBody);
        List<MultipartBody.Part> parts = builder.build().parts();
        toSubscribe(RetrofitManager.getInstance().create(ApiServer.class).uploadImage(parts), new DisposableObserver<BaseResponse<UploadImageBean>>() {
            @Override
            public void onNext(BaseResponse<UploadImageBean> uploadImageBeanBaseResponse) {
                hideProgress();
                if (uploadImageBeanBaseResponse.getCode() == 200) {
                    fontPhotoAddress = uploadImageBeanBaseResponse.getData().url;
                    imageAddress.put(fontPhoto, fontPhotoAddress);
                    uploadBackImage(backPhoto);
                } else {
                    showErrorTip(getApplication().getString(R.string.request_error));
                }
            }

            @Override
            public void onError(Throwable e) {

                hideProgress();
                showErrorTip(getApplication().getString(R.string.request_error));
            }

            @Override
            public void onComplete() {

            }
        });
    }

    public void uploadBackImage(String imagePath) {
        if (imagePath.contains("http")) {
            backPhotoAddress = imagePath;
            uploadData();
            return;
        }
        backPhotoAddress = imageAddress.get(backPhoto);
        if (!TextUtils.isEmpty(backPhotoAddress)) {
            uploadData();
            return;
        }
        showProgress();
        File file = new File(imagePath);
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        RequestBody logoRequestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        builder.addFormDataPart("file", file.getName(), logoRequestBody);
        List<MultipartBody.Part> parts = builder.build().parts();
        toSubscribe(RetrofitManager.getInstance().create(ApiServer.class).uploadImage(parts), new DisposableObserver<BaseResponse<UploadImageBean>>() {
            @Override
            public void onNext(BaseResponse<UploadImageBean> uploadImageBeanBaseResponse) {
                hideProgress();
                if (uploadImageBeanBaseResponse.getCode() == 200) {
                    backPhotoAddress = uploadImageBeanBaseResponse.getData().url;
                    imageAddress.put(backPhoto, backPhotoAddress);
                    uploadData();
                } else {
                    showErrorTip(getApplication().getString(R.string.request_error));
                }
            }

            @Override
            public void onError(Throwable e) {
                hideProgress();
                showErrorTip(getApplication().getString(R.string.request_error));
            }

            @Override
            public void onComplete() {

            }
        });
    }

    private void uploadData() {
        showProgress();
        Map<String, String> map = new HashMap<>();
        map.put("uid", String.valueOf(UserInfo.getUserInfo().id));
        map.put("username", username);
        map.put("idcardno", idcardno);
        map.put("idcardimg_p", fontPhotoAddress);
        map.put("idcardimg_n", backPhotoAddress);
        toSubscribe(RetrofitManager.getInstance().create(ApiServer.class).postAuthenData(map), new DisposableObserver<BaseResponse<String>>() {
            @Override
            public void onNext(BaseResponse<String> stringBaseResponse) {
                hideProgress();
                if (stringBaseResponse.getCode() == 200) {
                    finish();
                }
                if (!TextUtils.isEmpty(stringBaseResponse.getMsg())) {
                    ToastUtils.showShort(stringBaseResponse.getMsg());
                }
            }

            @Override
            public void onError(Throwable e) {
                hideProgress();
                showErrorTip(getApplication().getString(R.string.request_error));
            }

            @Override
            public void onComplete() {

            }
        });
    }
}
